package com.framework.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.framework.core.log.annotation.AutoLog;
import com.framework.core.result.R;
import com.framework.system.entity.dto.SysRoleAssignMenuDTO;
import com.framework.system.entity.dto.SysRoleAddDTO;
import com.framework.system.entity.dto.SysRoleQueryDTO;
import com.framework.system.entity.dto.SysRoleUpdateDTO;
import com.framework.system.entity.vo.SysDeptVO;
import com.framework.system.entity.vo.SysMenuVO;
import com.framework.system.entity.vo.SysRoleListVO;
import com.framework.system.entity.vo.SysRoleVO;
import com.framework.system.service.ISysRoleService;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * 角色管理
 *
 * @author Kevin
 * @since 2021-06-10
 */
@RestController
@RequestMapping("/system/sysRole")
@AllArgsConstructor
public class SysRoleController {

    private ISysRoleService sysRoleService;

    /**
     * 分页查询角色列表
     */
    @GetMapping("/selectSysRoleWithPage")
    @SaCheckPermission("system:sysRole:selectSysRoleWithPage")
    @AutoLog(module = "角色管理", title = "分页查询角色")
    public R<Page<SysRoleListVO>> selectSysRoleWithPage(Page reqPage, SysRoleQueryDTO req) {
        return R.success(sysRoleService.selectSysRoleWithPage(reqPage, req));
    }

    /**
     * 新增角色
     */
    @PostMapping("/addSysRole")
    @SaCheckPermission("system:sysRole:addSysRole")
    @AutoLog(module = "角色管理", title = "新增角色")
    public R<Void> addSysRole(@Validated @RequestBody SysRoleAddDTO req) {
        sysRoleService.addSysRole(req);
        return R.success();
    }

    /**
     * 修改角色
     */
    @PostMapping("/updateSysRole")
    @SaCheckPermission("system:sysRole:updateSysRole")
    @AutoLog(module = "角色管理", title = "修改角色")
    public R<Void> updateSysRole(@Validated @RequestBody SysRoleUpdateDTO req) {
        sysRoleService.updateSysRole(req);
        return R.success();
    }

    /**
     * 删除角色
     */
    @PostMapping("/removeSysRole")
    @SaCheckPermission("system:sysRole:removeSysRole")
    @AutoLog(module = "角色管理", title = "删除角色")
    public R<Void> removeSysRole(@RequestParam String ids) {
        sysRoleService.removeSysRole(ids);
        return R.success();
    }

    /**
     * 查看角色
     */
    @GetMapping("/getSysRole")
    @SaCheckPermission("system:sysRole:getSysRole")
    @AutoLog(module = "角色管理", title = "查看角色")
    public R<SysRoleVO> getSysRole(@RequestParam String id) {
        return R.success(sysRoleService.getSysRole(id));
    }

    /**
     * 导出角色
     *
     * @param req
     * @param response
     */
    @GetMapping("/exportSysRole")
    @SaCheckPermission("system:sysRole:exportSysRole")
    @AutoLog(module = "角色管理", title = "导出角色")
    @SneakyThrows
    public void exportSysRole(SysRoleQueryDTO req, HttpServletResponse response) {
        String filename = "角色列表" + "_" + System.currentTimeMillis() + ".xlsx";
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
        EasyExcel.write(response.getOutputStream())
                .head(SysRoleListVO.class)
                .sheet("角色列表")
                .doWrite(sysRoleService.selectSysRoleWithList(req));
    }

    /**
     * 分配菜单
     */
    @PostMapping("/assignMenu")
    @SaCheckPermission("system:sysRole:assignMenu")
    @AutoLog(module = "角色管理", title = "分配菜单")
    public R<Void> assignMenu(@Validated @RequestBody SysRoleAssignMenuDTO req) {
        sysRoleService.assignMenu(req);
        return R.success();
    }

    /**
     * 根据角色ID查询关联的菜单下拉树列表
     */
    @GetMapping("/selectSysMenuByRoleIdWithOptionTree")
    @AutoLog(module = "角色管理", title = "根据角色ID查询关联的菜单下拉树列表")
    public R<List<SysMenuVO>> selectSysMenuByRoleIdWithOptionTree(@RequestParam String roleId) {
        return R.success(sysRoleService.selectSysMenuByRoleIdWithOptionTree(roleId));
    }

    /**
     * 根据角色ID查询关联的部门下拉列表
     */
    @GetMapping("/selectSysDeptByRoleIdWithOption")
    @AutoLog(module = "角色管理", title = "根据角色ID查询关联的部门下拉列表")
    public R<List<SysDeptVO>> selectSysDeptByRoleIdWithOption(@RequestParam String roleId) {
        return R.success(sysRoleService.selectSysDeptByRoleIdWithOption(roleId));
    }

    /**
     * 查询角色下拉列表
     */
    @GetMapping("/selectSysRoleWithOption")
    @AutoLog(module = "角色管理", title = "查询角色下拉列表")
    public R<List<SysRoleVO>> selectSysRoleWithOption() {
        return R.success(sysRoleService.selectSysRoleWithOption());
    }

}

